package com.dashu.school.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories
public class DataConfig {

    @Autowired
    private HibernateProperties hibernateProperties;

    @Autowired
    @Qualifier("mainDataSource")
    private DataSource mainDataSource;

    @Autowired
    @Qualifier("tenantDataSource")
    private DataSource tenantDataSource;


    @Bean(name = "entityManagerMain")
    @Primary
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryMain(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerTenant")
    public EntityManager entityManager1(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryTenant(builder).getObject().createEntityManager();
    }


    @Autowired
    private JpaProperties jpaProperties;

    @Bean(name = "entityManagerFactoryMain")
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryMain(EntityManagerFactoryBuilder builder) {
        Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
        return builder
                .dataSource(mainDataSource)
                .properties(properties)
                .packages("com.dashu.school.model") //设置实体类所在位置,自行调整，如：com.test.entity
                .persistenceUnit("mainPersistenceUnit")
                .build();
    }


    @Autowired
    private JpaProperties jpaProperties1;

    @Bean(name = "entityManagerFactoryTenant")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryTenant(EntityManagerFactoryBuilder builder) {
        Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties1.getProperties(), new HibernateSettings());

        LocalContainerEntityManagerFactoryBean bean = builder
                .dataSource(tenantDataSource)
                .properties(properties)
                .packages("com.dashu.school.modelat") //设置实体类所在位置,同数据源相似
                .persistenceUnit("tenantPersistenceUnit")
                .build();
        bean.setJpaPropertyMap(new HashMap<>());
        return bean;
    }


    @Bean(name = "transactionManagerMain")
    @Primary
    PlatformTransactionManager transactionManagerMain(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryMain(builder).getObject());
    }


    @Bean(name = "transactionManagerTenant")
    PlatformTransactionManager transactionManagerTenant(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryTenant(builder).getObject());
    }

}
